<template>

  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :rules="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="企业名称" prop="compName">
        <el-input
          v-model="queryParams.compName"
          placeholder="请输入企业名称"
          maxlength="30"
          show-word-limit
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="会议室名称" prop="broadroomName" label-width="100">
        <el-input
          v-model="queryParams.broadroomName"
          placeholder="请输入会议室名称"
          maxlength="30"
          show-word-limit
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入联系人姓名"
          maxlength="30"
          show-word-limit
          clearable
          size="small"
        />
      </el-form-item>

      <el-form-item label="预定时间段" label-width="100">
        <el-date-picker
          v-model="dateRange"
          size="small"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button type="info" icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['server:book:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['server:book:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bookList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center" type="index" key="broadroomBookId" prop="broadroomBookId"
                       v-if="columns[0].visible"/>
      <el-table-column label="会议室名称" align="center" key="broadroomName" prop="broadroomName" v-if="columns[1].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="企业名称" align="center" key="compName" prop="compName" v-if="columns[2].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="联系人" align="center" key="userName" prop="userName" v-if="columns[3].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="会议地点" align="center" key="broadroomAddress" prop="broadroomAddress"
                       v-if="columns[4].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="开始时间" align="center" key="approverName" prop="bookStart" v-if="columns[5].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="结束时间" align="center" key="approverTime" prop="bookEnd" width="150"
                       v-if="columns[6].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="审批状态" align="center" key="approverState" prop="approverState" :formatter="statusFormat"
                       v-if="columns[7].visible"
                       :show-overflow-tooltip="true"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <div v-if="scope.row.approverState === '0'">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-check"
              @click="handlePass(scope.row)"
              v-hasPermi="['server:book:edit']"
            >通过
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-close"
              @click="handleRefuse(scope.row)"
              v-hasPermi="['server:book:edit']"
            >拒绝
            </el-button>
          </div>
          <div v-else>
            <el-button
              align="center"
              size="mini"
              type="text"
              icon="el-icon-view"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['server:book:query']"
            >查看
            </el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 查看会议室预约表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form :model="form" label-width="125px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="会议室名称："> {{ form.broadroomName }}</el-form-item>
            <el-form-item label="企业名称："> {{ form.compName }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="会议地点："> {{ form.broadroomAddress }}</el-form-item>
            <el-form-item label="预约时间段">{{form.bookStart}}~{{form.bookEnd}}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系人："> {{ form.userName }}</el-form-item>
            <el-form-item label="审批时间：">
              {{ form.approverTime }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="审批人："> {{ form.approverName }}</el-form-item>
            <el-form-item label="审批状态：">
              {{selectDictLabel(statusOptions, form.approverState)}}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-dialog>
    <!-- 查看会议室预约表对话框 -->

  </div>
</template>

<script>

import {addJob, updateJob} from '@/api/monitor/job'
import {delBook, exportBook, getBook, listBook, updateBook} from '@/api/server/broadroom/book'
import Cookies from 'js-cookie'
import {parseTime} from "../../../utils/project";
import {listBookTime} from "../../../api/server/broadroom/book";

export default {
  name: 'Job',
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids:[],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约表格数据
      bookList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 是否显示详细弹出层
      openView: false,
      // 状态字典
      statusOptions: [],
      dateRange: '',
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      //列信息
      columns: [
        {key: 0, label: "序号", visible: true},
        {key: 1, label: "会议室名称", visible: true},
        {key: 2, label: "企业名称", visible: true},
        {key: 3, label: "联系人", visible: true},
        {key: 4, label: "会议地点", visible: true},
        {key: 5, label: "审批人", visible: true},
        {key: 6, label: "审批时间", visible: true},
        {key: 7, label: "审批状态", visible: true},
      ],
      queryForm: {
        broadroomName: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
        compName: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
        userName: [{ pattern: /^[\u4E00-\u9FA5A-Za-z0-9]+$/, message: '不能为特殊字符', trigger: 'blur'}],
      },
      // 表单校验
      rules: {
        // jobName: [
        //   {required: true, message: "任务名称不能为空", trigger: "blur"}
        // ],
      }
    }
  },
  created() {
    this.getList()
    this.getDicts('t_pass_status').then(response => {
      this.statusOptions = response.data
    })
  },
  methods: {
    /** 查询定时任务列表 */
    getList() {
      this.loading = true
      listBook(this.addDateRange1(this.queryParams, this.dateRange)).then(response => {
        this.bookList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.approverState)
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()

    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.$refs['queryForm'].validate(valid => {
        if (valid) {
          this.getList()
        }
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams.params.beginTime = ''
      this.queryParams.params.endTime = ''
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 表单重置
    reset() {
      this.form = {
        deptName: '',
        userName: '',
        broadroomName: '',
        broadroomAddress: '',
        approverName: '',
        approverTime: '',
        approverState: ''
      }
      this.resetForm('form')
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.broadroomBookId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 拒绝按钮操作 */
    handleRefuse: function (row) {
      const ids = row.broadroomBookId || this.ids
      this.$confirm('是否拒绝"' + row.broadroomName + '"的预约请求?', '提示', {
        cancelButtonClass: 'btn-custom-cancel',
        closeOnClickModal: false,
        type: 'info'
      }).then(function () {
        const approverName = Cookies.get('user.username')
        let date = parseTime(new Date())
        let data = {
          broadroomBookId: row.broadroomBookId,
          approverState: '2',
          approverTime: date
        }
        return updateBook(data)
      }).then(() => {
        this.getList()
        this.msgSuccess('已拒绝')
      })
    },

    /** 通过按钮操作 */
    handlePass: function (row) {
      let date = parseTime(new Date())
      // const approverName = Cookies.get('username')
      let data = {
        broadroomBookId: row.broadroomBookId,
        approverState: '1',
        approverTime: date,
        broadroomId:row.broadroomId,
        bookStart:row.bookStart,
        bookEnd:row.bookEnd,
      }
      listBookTime(data).then((res) => {
        console.log(res)
        if (res.data.length > 0){
          this.$confirm('该时间段'+(row.broadroomName) +'已被预约成功，是否拒绝？', '提示', {
            cancelButtonClass: 'btn-custom-cancel',
            closeOnClickModal: false,
            type: 'info'
          }).then(function () {
              data.approverState= '2'
            return updateBook(data)
          }).then(() => {
            this.getList()
            this.msgSuccess('已拒绝')
          })
        }
        else {
          this.$confirm('确认通过该时间段'+(row.broadroomName) +'的预约请求', '提示', {
            cancelButtonClass: 'btn-custom-cancel',
            closeOnClickModal: false,
            type: 'info'
          }).then(function () {
              data.approverState='1'
            return updateBook(data)
          }).then(() => {
            this.getList()
            this.msgSuccess('已通过')
          })
        }
      })
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.broadroomBookId
      getBook(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '查看预约表'
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.jobId != undefined) {
            updateJob(this.form).then(response => {
              this.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addJob(this.form).then(response => {
              this.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const broadroomBookId = row.broadroomBookId || this.ids
      this.$confirm('是否确认删除该预定数据项?', '警告', {
        cancelButtonClass: 'btn-custom-cancel',
        closeOnClickModal: false,
        type: 'warning'
      }).then(function () {
        return delBook(broadroomBookId)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams
      this.$confirm('是否确认导出所有预约表数据项?', '提示', {
        cancelButtonClass: 'btn-custom-cancel',
        closeOnClickModal: false,
        type: 'info'
      }).then(function () {
        return exportBook(queryParams)
      }).then(response => {
        this.download(response.msg)
      })
    }
  }
}
</script>
